package com.itheima.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberMapper;
import com.itheima.dao.OrderMapper;
import com.itheima.dao.OrderSettingMapper;
import com.itheima.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Result submit(OrderInfoVo orderInfoVo) {
        Date orderDate = orderInfoVo.getOrderDate();
        String telephone = orderInfoVo.getTelephone();
        String idCard = orderInfoVo.getIdCard();
        String name = orderInfoVo.getName();
        String orderType = orderInfoVo.getOrderType();
        Integer setmealId = orderInfoVo.getSetmealId();
        Integer sex = orderInfoVo.getSex();
        //1.核对预约该天是否有预约设置
        Integer count = orderSettingMapper.queryByDate(orderDate);
        if (count == 0) {
            return Result.error("当天无预约信息");
        }
        //2.核对预约人数是否已满
        OrderSetting orderSetting = orderSettingMapper.queryOrderSettingByDate(orderDate);
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations >= number) {
            return Result.error("预约已满");
        }

        /*//3.核对用户是否为会员
        Member member = memberMapper.findByTelephone(telephone);

=======
        //3.核对用户是否为会员
        Member member = memberMapper.findByTelephone(telephone);
>>>>>>> origin/wujun
        //4.若不是会员 ，注册会员
        if (member == null) {
            member = new Member();
            member.setName(name);
            member.setSex(String.valueOf(sex));
            member.setIdCard(idCard);
            member.setPhoneNumber(telephone);
            member.setRegTime(orderDate);
            memberMapper.add(member);
<<<<<<< HEAD
        }*/
        //5.判断该用户是否已预约
        /*Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderDate);*/
/*        List<Order> orders = orderMapper.findByCondition(order);
        if (CollectionUtil.isNotEmpty(orders)) {
            return Result.error("已存在预约");
        }*/
        Member member = orderMapper.findByName(name);
        if (member == null) {
            member = new Member();
            member.setName(name);
            member.setPhoneNumber(telephone);
            member.setRegTime(orderDate);
            memberMapper.add(member);
        }
        Integer memberId = member.getId();
        Order order = new Order();
        order.setMemberId(memberId);
        order.setOrderDate(orderDate);
     List<Order> orders = orderMapper.findByCondition(order);
        if (CollectionUtil.isNotEmpty(orders)) {
            return Result.error("已存在预约");
        }

        //6.添加订单信息
        order = new Order();
        order.setMemberId(memberId);
        order.setOrderDate(orderDate);
        order.setOrderType(orderType);
        order.setSetmealId(setmealId);
        order.setOrderStatus("未到诊");
        orderMapper.add(order);
        //7.预约人数加一

        Integer edit = orderSettingMapper.edit(orderSetting);
        if (edit == 0) {
            throw new RuntimeException("非法请求,请刷新重试");
        }

        return Result.success("预约成功", order.getId());
    }

    @Override
    public Map findById4Detail(Integer id) {
        return orderMapper.findById4Detail(id);
    }

    @Override
    public Result mobileAdd(MobileOrderVo mobileOrderVo) {
        Date orderDate = mobileOrderVo.getOrderDate();
        String phoneNumber = mobileOrderVo.getPhoneNumber();
        String name = mobileOrderVo.getName();
        String orderType = mobileOrderVo.getOrderType();
        Integer setmealId = mobileOrderVo.getSetmeal_id();
        //核对改天是否有预约设置
        Integer count = orderSettingMapper.queryByDate(orderDate);
        if (count == 0){
            return Result.error("当天无预约信息");
        }
        //核对预约的人数是否已满
        OrderSetting orderSetting = orderSettingMapper.queryOrderSettingByDate(orderDate);
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations>=number){
            return Result.error("预约已满");
        }
        //核对用户是否为会员
        Member member = memberMapper.findByTelephone(phoneNumber);
        if (member == null){
            member = new Member();
            member.setName(name);
            member.setPhoneNumber(phoneNumber);
            member.setRegTime(orderDate);
            memberMapper.add(member);
        }
        //判断该用户是否已预约
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderDate);
        List<Order> orders = orderMapper.findByCondition(order);
        if (CollectionUtil.isNotEmpty(orders)){
            return Result.error("已存在预约");
        }
        order.setOrderType(orderType);
        order.setSetmealId(setmealId);
        order.setOrderStatus("未到诊");
        orderMapper.add(order);
        //预约人数加一
        orderSetting.setReservations(orderSetting.getReservations()+1);
        return Result.success("预约成功",order.getId());

    }

    @Override
    public void deleteById(int id) {
        orderMapper.deleteById(id);
    }

    @Override
    public MobileOrderVo findById(int id) {
        return orderMapper.findById(id);
    }

    @Override
    public void edit(Order order) {
        orderMapper.edit(order);
    }
}
